package com.qingcloud.df.executor.engine.context;

import com.qingcloud.df.executor.process.common.DaemonProcessFactory;
import com.qingcloud.df.executor.process.docker.DockerProcessFactory;
import com.qingcloud.df.executor.process.ProcessFactory;
import com.qingcloud.df.executor.process.dockercompose.DockerComposeProcessFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


@Component
public class ContainerEngineBeanFactory {

    @Value("${qingcloud.executor.instance.container.network}")
    private String dockerNetwork;
    @Value("${qingcloud.executor.instance.container.dataPath}")
    private String dataMount;
    @Value("${qingcloud.executor.instance.container.configPath}")
    private String configMount;

    @ConditionalOnExpression("'${qingcloud.executor.instance.runtime}'.equals('docker')")
    @Bean("processFactory")
    public ProcessFactory dockerProcessFactory(){

        return createDockerProcessFactory(dataMount,configMount, dockerNetwork);
    }
    @ConditionalOnExpression("'${qingcloud.executor.instacne.runtime}'.equals('kube')")
    @Bean("processFactory")
    public ProcessFactory k8sProcessFactory(){

        return createDockerProcessFactory(dataMount,configMount, dockerNetwork);
    }

    @ConditionalOnExpression("'${qingcloud.executor.instacne.runtime}'.equals('daemon')")
    @Bean("processFactory")
    public ProcessFactory daemonProcessFactory(){

        return new DaemonProcessFactory();
    }

    @ConditionalOnExpression("'${qingcloud.executor.instacne.runtime}'.equals('dockercompose')")
    @Bean("processFactory")
    public ProcessFactory dockerComposeProcessFactory(){

        return new DockerComposeProcessFactory();
    }


    private ProcessFactory createDockerProcessFactory(final String dataMount,
                                                      final String configMount,
                                                      final String dockerNetwork) {
        return new DockerProcessFactory(dockerNetwork);
    }
}
